package com.hopu.blb_erp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopu.blb_erp.entity.PageEntity;
import com.hopu.blb_erp.entity.SCompany;
import com.hopu.blb_erp.entity.TPar;
import com.hopu.blb_erp.mapper.TParMapper;
import com.hopu.blb_erp.service.ISCompanyService;
import com.hopu.blb_erp.service.TParService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TParServiceImpl extends ServiceImpl<TParMapper, TPar> implements TParService {

    @Autowired
    private TParMapper tParMapper;

    @Override
    public PageEntity<TPar> findAPage(Integer id, Integer current, Integer limit, String startTime, String endTime) {
        int count = Math.toIntExact(tParMapper.selectCount(new QueryWrapper<TPar>().eq("t_par_com_a",id)
                .ge(StringUtils.isNotBlank(startTime),"t_par_time",startTime)
                .le(StringUtils.isNotBlank(endTime),"t_par_time",endTime)
                .or().like(StringUtils.isNotBlank(endTime),"t_par_time",endTime)));
        int pageSize = count % limit == 0 ? count / limit : count / limit + 1;
        List<TPar> tPars = tParMapper.selectTParA((current - 1) * limit,limit,id, startTime, endTime);
        PageEntity<TPar> page = new PageEntity<>();
        page.setCount(count);
        page.setData(tPars);
        page.setPageSize(pageSize);
        page.setCurrent(current);
        return page;
    }

    @Override
    public PageEntity<TPar> findBPage(Integer id, Integer current, Integer limit, String name, String time) {
        int count = tParMapper.selectLikeCount(id,time,name);
        int pageSize = count % limit == 0 ? count / limit : count / limit + 1;
        List<TPar> tPars = tParMapper.selectTParB((current - 1) * limit,limit,id,name,time);
        PageEntity<TPar> page = new PageEntity<>();
        page.setCount(count);
        page.setData(tPars);
        page.setPageSize(pageSize);
        page.setCurrent(current);
        return page;
    }
}
